package edu.byui.chat;

import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	protected String fileLocation = "C:\\test.chat";
	protected Chat chat;
	protected boolean keepAlive;
	private ServerSocket servSocket;

	public static void main(String args[]) {
		@SuppressWarnings("unused")
		Server s = new Server();
	}

	public Server() {
		chat = new Chat(this.fileLocation);
		try {
			servSocket = new ServerSocket(5555);		
		}
		catch(Exception e) {
			System.out.println("The following error has occurred with the Server:");
			e.printStackTrace();
		}

		handleConnections();
		System.out.println("Server is shutting down...");
		System.out.println("[Server Shut Down]");
	}
	public void handleConnections() {
		while(true) {
			try {
				System.out.println("Waiting for connection");
				Socket receivedSocket = servSocket.accept();
				System.out.println("Connection Request made...");
				System.out.println("From: " + receivedSocket.toString());
				Thread t = new Thread(new ServerThread(receivedSocket, chat, fileLocation));
				t.start();
				System.out.println("Connection made from a client.");
			}
			catch(Exception e) {
				System.out.println("The following error has occurred with the Server:");
				e.printStackTrace();
			}
		}
	}
}